<?php

namespace UI\Admin;

class Table {
	public $id = '';
	public $headers = array();
	public $page = '';
	public $rows = array();
	public $row_index = 0;

	public function __construct($id = null, $configs = array()) {
		foreach ($configs as $field => $val) {
			$this->{$field} = $val;
		}

		$this->id = is_null($id) ? uniqid() : $id;

		return $this;
	}

	public static function forge($id = null, $configs = array()) {
		return new Table($id, $configs);
	}

	public function add_header($name, $atts = array()) {
		$this->headers[] = array(
			$name,
			$atts
		);
		return $this;
	}

	public function next() {
		$this->row_index++;
		return $this;
	}

	public function add_col($content, $atts = array()) {
		$this->rows[$this->row_index][] = array(
			$content,
			$atts
		);
		return $this;
	}

	public function add_col_html($content = '') {
		$this->rows[$this->row_index][] = $content;
	}

	public function add_page($content) {
		$this->page = $content;
	}

	public function html() {
		$html = '<div class="table-container">';
		$html .= '<table id="' . $this->id . '" class="table-list" cellpadding="0" cellspacing="0" border="0" width="100%">';
		// get headers
		$html .= $this->get_headers();
		// get rows
		$html .= $this->get_rows();
		$html .= '</table>';
		// pagination
		if ($this->page !== '') {
			$html .= '<div class="pages">' . $this->page . '</div>';
		}
		$html .= '</div>';

		$html .= $this->get_script();

		return $html;
	}

	public function get_headers() {
		$out = '<thead>';
		$out .= '<tr class="row row-head">';
		foreach ($this->headers as $header) {
			list($title, $atts) = $header;
			$out .= '<th ' . array_to_attr($atts) . '>' . $title . '</th>';
		}
		$out .= '</tr>';
		$out .= '</thead>';

		return $out;
	}

	public function get_rows() {
		$out = '<tbody>';
		foreach ($this->rows as $row_index => $row) {
			$out .= '<tr class="row row-item row-' . ($row_index + 1) . '">';
			foreach ($row as $col_index => $col) {
				if (is_array($col)) {
					list($content, $atts) = $col;
					$out .= '<td class="col-' . ($col_index + 1) . '" ' . array_to_attr($atts) . '>' . $content . '</td>';
				}
				else {
					$out .= $col;
				}
			}
			$out .= '</tr>';
		}
		$out .= '</tbody>';

		return $out;
	}

	private function get_script() {
		return "<script type=\"text/javascript\">
			$('#{$this->id}_actions').hide();
			// check all
			$('table#{$this->id} .checkall').on('click', function() {
				var checked = $(this).is(':checked');
				$(this).parents('#{$this->id}').find('input[type=checkbox].check-item').not(this).each(function() {
					var item = $(this).prop('checked', checked).uniform();
				});
				if(checked) {
					$('#{$this->id}_title').hide();
					$('#{$this->id}_actions').css({
						opacity: 0.5,
						marginLeft: '30px'
					}).show().animate({
						marginLeft: '0px',
						opacity: 1
					}, 300);
				} else {
					$('#{$this->id}_actions').hide();
					$('#{$this->id}_title').css({
						opacity: 0.5,
						marginLeft: '30px'
					}).show().animate({
						marginLeft: '0px',
						opacity: 1
					}, 300);
				}
			});
			$('.check-item').on('click', function() {
				var items_checked = $('table#{$this->id} input.check-item:checked').length;
				var items_length = $('table#{$this->id} input.check-item').length;
				$('table#{$this->id} input.checkall:first-child').prop('checked', items_checked == items_length).uniform();

				if(items_checked > 0) {
					if($('#{$this->id}_title').css('display') !== 'none') {
						$('#{$this->id}_title').hide();
						$('#{$this->id}_actions').css({
							opacity: 0.5,
							marginLeft: '30px'
						}).show().animate({
							marginLeft: '0px',
							opacity: 1
						}, 300);
					}
				} else {
					if($('#{$this->id}_actions').css('display') !== 'none') {
						$('#{$this->id}_actions').hide();
						$('#{$this->id}_title').css({
							opacity: 0.5,
							marginLeft: '30px'
						}).show().animate({
							marginLeft: '0px',
							opacity: 1
						}, 300);
					}
				}
			});
		</script>";
	}

}
